package com.pds.web.monitor.controller;


import com.pds.common.core.controller.BaseController;
import com.pds.common.core.domain.R;
import com.pds.common.core.page.TableDataInfo;
import com.pds.web.monitor.domain.PdsMonitorTask;
import com.pds.web.monitor.domain.PdsMonitorTaskReport;
import com.pds.web.monitor.dto.TaskDTO;
import com.pds.web.monitor.service.IPdsMonitorTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/task")
public class PdsMonitorTaskController extends BaseController {

    @Autowired
    private IPdsMonitorTaskService pdsMonitorTaskService;


    /**
     * 新增任务
     * @param pdsMonitorTask
     * @return
     */
    @PreAuthorize("@ss.hasPermi('detection:task:add')")
    @PostMapping("/addTask")
    public R<PdsMonitorTask> add(@RequestBody PdsMonitorTask pdsMonitorTask){
        pdsMonitorTaskService.addTask(pdsMonitorTask);
        return R.ok();
    }

    /**
     * 查询全部任务 -- 分页/条件:status,taskName,taskStartDate,taskEndDate
     * @param taskDTO -- 任务状态，任务名称，任务开始结束时间
     * @return
     */
    @GetMapping("/listAll")
    public TableDataInfo listAll(TaskDTO taskDTO) throws Exception {
        startPage();
        List<Map> list = pdsMonitorTaskService.listAll(taskDTO);
        return getDataTable(list);
    }


    /**
     * 根据id查询任务
     * @param id -- 主键
     * @return
     */
    @GetMapping("/queryById")
    public R queryById(@RequestParam Long id){
        return R.ok(pdsMonitorTaskService.queryById(id));
    }

    /**
     * 检测机构-检测任务负责人,接收任务、分配人员
     * @param map
     *          -taskId 任务ID
     *          -taskCode 任务编码
     *          -guidanceId 检测因子ID，多个逗号分割
     *          -samplerId 采样人员id
     *          -analysisId 分析人员id
     *          reportWriterId 报告撰写人id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('detection:task:receive')")
    @PostMapping("/receiveTask")
    public R receiveTask(@RequestBody Map map) throws Exception {
        pdsMonitorTaskService.receiveTask(map);
        return R.ok();
    }

    /**
     * 删除任务
     * @param id -- 主键
     * @return
     */
    @GetMapping("/deleteTask")
    public R delete(@RequestParam Long id) throws Exception {
        return R.ok(pdsMonitorTaskService.deleteTask(id));
    }


    /**
     * 变更人员配置
     * @param map
     * @return
     */
    @PostMapping("/changeUser")
    public R changeUser(@RequestBody Map map) throws Exception {
        pdsMonitorTaskService.changeUser(map);
        return R.ok();
    }



    /**
     * 采样提交
     */
    @PreAuthorize("@ss.hasPermi('detection:task:sampling')")
    @PostMapping("/samplingSubmit")
    public R samplingSubmit(@RequestBody PdsMonitorTaskReport report) throws Exception {
        pdsMonitorTaskService.samplingSubmit(report,"2");
        return R.ok();
    }


    /**
     * 实验室分析提交
     */
    @PreAuthorize("@ss.hasPermi('detection:task:analyst')")
    @PostMapping("/analysisSubmit")
    public R analysisSubmit(@RequestBody PdsMonitorTaskReport report) throws Exception {
        pdsMonitorTaskService.samplingSubmit(report,"3");
        return R.ok();
    }

    /**
     * 检测报告提交
     */
    @PreAuthorize("@ss.hasPermi('detection:task:report')")
    @PostMapping("/reportSubmit")
    public R reportSubmit(@RequestBody PdsMonitorTaskReport report) throws Exception {
        pdsMonitorTaskService.samplingSubmit(report,"4");
        return R.ok();
    }

    /**
     * 环保局审核
     * @param map
     *          - taskId 任务id
     *          - type 1审核通过、2审核退回
     *          - epbScore 评分
     *          - remark 退回原因
     */
    @PreAuthorize("@ss.hasPermi('detection:task:check')")
    @PostMapping("/approve")
    public R approve(@RequestBody Map map) throws Exception {
        pdsMonitorTaskService.approve(map);
        return R.ok();
    }


    /**
     * 审核退回时，修改任务内容
     * @param map
     *          -taskId					任务id
     *          -taskName				任务名称
     *          -taskCode				任务编号
     *          -contractFileId			合同附件ID
     *          -guidanceId				检测因子
     *          -samplingRecordFileId	采样记录文件id,多个用逗号分割
     *          -sceneAnalysisFileId	现场分析记录文件id,多个用逗号分割
     *          -laboratAnalysisFileId	实验室分析记录文件id,多个用逗号分割
     *          -reportResultFileId		检测报告文件id,多个用逗号分割
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('detection:task:report')")
    @PostMapping("/edit")
    public R edit(@RequestBody Map map) throws Exception {
        pdsMonitorTaskService.edit(map);
        return R.ok();
    }

    /**
     * 企业给检测任务评分
     * @param map
     *          - taskId 任务id
     *          - enScore 企业评分
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('detection:task:add')")
    @PostMapping("/enScore")
    public R enScore(@RequestBody Map map) throws Exception {
        pdsMonitorTaskService.enScore(map);
        return R.ok();
    }

    /**
     * web端首页待办查询
     * 检查任务
     * 运维任务
     * @return
     */
    @GetMapping("getTask")
    public R getTask() throws Exception {
        Map map = pdsMonitorTaskService.getTask();
        return R.ok(map);
    }

}
